package com.jeenms.jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.time.DateFormatUtils;

/**
 * 
 * @author lilz-2686
 *
 */
public class BatchTestTidb {

	/**
	 * @param args
	 * @throws SQLException
	 */

	// INSERT INTO csc_parking_cloud.csc_park_record (year, month, day, hour,
	// car_number, lot_id, channel_id, in_time, out_time, enter_type, pay_money)
	// VALUES ('2019', '1', '1', '1', 'A001', '0001', '1', NULL, NULL, '0', '0');

	static void createBatch(int count) throws SQLException {
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		conn = JdbcUtilsTidb.getConnection();
		String insertSql = "INSERT INTO csc_park_record (year, month, day, hour, car_number, lot_id, channel_id, in_time, out_time, enter_type, pay_money)";
		insertSql += "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
		stmt = conn.prepareStatement(insertSql);

		try {
			conn.setAutoCommit(false);
			for (int i = 1; i <= count; i++) {
				Date randomDate = randomDate("2017-01-01", "2019-07-21");
				int lot_id = RandomUtils.nextInt(1, 2000);
				int channel_id = RandomUtils.nextInt(1, 11);// 1~10;
				Date in_time = randomDate;
				int year = in_time.getYear() + 1900;
				int month = in_time.getMonth() + 1;
				int day = in_time.getDate();
				int hour = in_time.getHours();
				String car_number = "A" + RandomUtils.nextInt(10000, 99999);
				int enter_type = RandomUtils.nextInt(0, 2);// 0,1
				int pay_money = RandomUtils.nextInt(0, 100);//

				stmt.setInt(1, year);
				stmt.setInt(2, month);//
				stmt.setInt(3, day);
				stmt.setInt(4, hour);
				stmt.setString(5, car_number);// car_number
				stmt.setInt(6, lot_id);// lot_id

				stmt.setInt(7, channel_id);// channel_id
				//System.out.println(in_time);
				String pattern = "yyyy-MM-dd HH:mm:ss";
				
				stmt.setString(8, DateFormatUtils.format(in_time, pattern));// in_time
				stmt.setString(9, DateFormatUtils.format(in_time, pattern));// in_time
				//stmt.setTimestamp(8, new java.sql.Timestamp(in_time.getTime()));// in_time
				//stmt.setTimestamp(9, new java.sql.Timestamp(in_time.getTime() + RandomUtils.nextInt(10, 1000) * 60000));// out_time
				stmt.setInt(10, enter_type);// enter_type
				stmt.setInt(11, pay_money);// pay_money
				stmt.execute();
				if (i % 5000 == 0) {
					conn.commit();
					System.out.println(i);
					conn.setAutoCommit(false);
				}
			}
		}catch (Exception e) {
			e.printStackTrace();
		} 
		finally {
			JdbcUtilsTidb.free(rs, stmt, conn);
		}
	}

	//
	/**
	 * 获取随机日期
	 * 
	 * @param beginDate 起始日期，格式为：yyyy-MM-dd
	 * @param endDate   结束日期，格式为：yyyy-MM-dd
	 * @return
	 */
	private static Date randomDate(String beginDate, String endDate) {
		try {
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			Date start = format.parse(beginDate); // 构造开始日期
			Date end = format.parse(endDate); // 构造结束日期
			// getTime()表示返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数。
			if (start.getTime() >= end.getTime()) {
				return null;
			}

			long date = random(start.getTime(), end.getTime());

			return new Date(date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	private static long random(long begin, long end) {
		long rtn = begin + (long) (Math.random() * (end - begin));
		// 如果返回的是开始时间和结束时间，则递归调用本函数查找随机值
		if (rtn == begin || rtn == end) {
			return random(begin, end);
		}
		return rtn;
	}

	public static void main(String[] args) throws SQLException {
		long start = System.currentTimeMillis();

		start = System.currentTimeMillis();
		//createBatch(5000000);//500万
		  createBatch(50000000);//5000万
		long end = System.currentTimeMillis();
		System.out.println("createBatch:" + (end - start));
	}
}
